<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Member extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('android');
        $this->load->library('parser');
        $log = $this->session->all_userdata();
        if (!isset($log) || $log['login'] != TRUE || $log['privilege'] != 2) {
            redirect('main/logout');
        }
    }

    function trueLogin() {
        $log = $this->session->all_userdata();
        if (!isset($log) || $log['login'] != TRUE || $log['privilege'] != 2) {
            redirect('main/logout');
        }
    }

    function index() {
        redirect('member/home');
    }

    function home() {
        $this->trueLogin();
        $data = array(
            'blog_title' => 'Home member',
            'alert' => 'Registrasi berhasil. Selamat datang ' . $this->session->userdata('username')
        );
        $this->parser->parse('themes/head', $data);
        $this->load->view('themes/open_container');
        $this->load->view('member/nav');
        $this->parser->parse('member/tes', $data);
        $this->load->view('themes/close_container');
        $this->load->view('themes/footer');
    }

    function upload() {
        $this->trueLogin();
        $data = array(
            'blog_title' => 'Upload file'
        );
        $this->parser->parse('themes/head', $data);
        $this->load->view('themes/open_container');
        $this->load->view('member/nav');
        $this->load->view('member/upload');
        $this->load->view('themes/close_container');
        $this->load->view('themes/footer');
    }

    function uploadFile() {
        $this->trueLogin();
        $config['upload_path'] = './build/';
        $config['allowed_types'] = 'zip';
        $config['max_size'] = '1000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload()) {
//            $error = array('error' => $this->upload->display_errors());
//            redirect('member', $error);
            echo $this->upload->display_errors();
        } else {
            $file = $this->upload->data();
            $data = $this->session->all_userdata();
            $input = array(
                'username' => $data['username'],
                'file_name' => $file['file_name'],
                'full_path' => base_url() . 'build/' . $file['file_name']
            );
            $this->android->filaUpload($input);
            redirect('member/listFile');
        }
    }

    function listFile() {
        $this->trueLogin();
        $data = array(
            'blog_title' => 'List File'
        );
        $id = $this->session->userdata('username');
        if ($this->android->listFile($id)) {
            $data['file'] = $this->android->listFile($id);
        } else {
            $data['file'] = array();
        }
        $this->parser->parse('themes/head', $data);
        $this->load->view('themes/open_container');
        $this->load->view('member/nav');
        $this->load->view('member/listFile', $data);
        $this->load->view('themes/close_container');
        $this->load->view('themes/footer');
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
